package com.mytest;


import com.neardrug.NearDrugApplication;
import com.neardrug.entity.FormImg;
import com.neardrug.entity.NumeratorPo;
import com.neardrug.mapper.FormImgMapper;
import com.neardrug.utils.ApiUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

//@SpringBootTest(classes = NearDrugApplication.class)
public class MyTest1 {
    @Resource
    private FormImgMapper formImgMapper;

    @Test
    public void test1() {
        try {
            String html = "<table><thead><tr><th>Compound</th><th>1-6 h</th><th>6-12 h</th><th>12-18 h</th><th>1-6 h</th><th>6-12 h</th><th>12-18 h</th></tr></thead><tbody><tr><td>1</td><td>-15.4</td><td>-6.2</td><td>-0.6</td><td>-14.0</td><td>-6.4</td><td>-3.2</td></tr><tr><td>5</td><td>-2.4</td><td>-2.6</td><td>-2.8</td><td>-3.6</td><td>-1.7</td><td>-2.6</td></tr><tr><td>9</td><td>-11.8</td><td>-6.6</td><td>1.0</td><td>-10.6</td><td>-5.2</td><td>-2.5</td></tr><tr><td>10</td><td>-5.3</td><td>-0.9</td><td>-3.1</td><td>0.6</td><td>3.4</td><td>3.8</td></tr><tr><td>11</td><td>-9.3</td><td>-3.3</td><td>3.9</td><td>-6.9</td><td>-2.3</td><td>0.9</td></tr><tr><td>12</td><td>-11</td><td>-5.8</td><td>3.3</td><td>-12</td><td>-5.8</td><td>0.8</td></tr></tbody></table>";
            // 检查HTML字符串的原始结构
            if (!isValidHtmlTableStructure(html)) {
                System.out.println("HTML表格结构不合法");
                return;
            }

            // 解析HTML字符串
            Document document = Jsoup.parse(html);

            // 获取根节点
            Element root = document.body().child(0);
            if (!root.tagName().equals("table")) {
                System.out.println("根节点不是<table>");
                return;
            }

            // 检查<table>内部的结构
            Elements rows = root.select("tr");
            if (rows.size() == 0) {
                System.out.println("没有<tr>标签");
                return;
            }

            for (Element row : rows) {
                Elements cells = row.select("td, th");
                if (cells.size() == 0) {
                    System.out.println("<tr>中没有<td>或<th>");
                    return;
                }
            }

            System.out.println("ok");
        } catch (Exception e) {
            System.out.println("异常");
        }
    }

    // 检查HTML表格字符串的基本结构
    private boolean isValidHtmlTableStructure(String html) {
        // 检查是否包含<table>和</table>
        if (!html.contains("<table") || !html.contains("</table>")) {
            return false;
        }

        // 检查<tr>标签是否正确闭合
        int trOpenCount = html.split("<tr>", -1).length - 1;
        int trCloseCount = html.split("</tr>", -1).length - 1;
        if (trOpenCount != trCloseCount) {
            return false;
        }

        // 检查<th>和<td>标签是否正确闭合
        int thOpenCount = html.split("<th>", -1).length - 1;
        int thCloseCount = html.split("</th>", -1).length - 1;
        int tdOpenCount = html.split("<td>", -1).length - 1;
        int tdCloseCount = html.split("</td>", -1).length - 1;

        if (thOpenCount != thCloseCount || tdOpenCount != tdCloseCount) {
            return false;
        }

        return true;
    }


    @Test
    public void testSaveBase64ImageToLocalFolder() throws IOException {
        List<FormImg> formImgList = formImgMapper.getByTaskId(200);
        FormImg formImg = formImgList.get(0);
        // 示例 Base64 编码的图片字符串
        String base64Image =formImg.getImg();
        // 解码 Base64 字符串
        byte[] imageBytes = Base64.getDecoder().decode(base64Image);

        // 定义保存图片的文件夹路径（相对于项目根目录）
        String folderPath = "files"; // 例如在项目根目录下创建一个名为 "files" 的文件夹
        String fileName = "test-image.png"; // 定义图片文件名

        // 创建文件夹（如果不存在）
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        // 定义完整的文件路径
        String filePath = folderPath + File.separator + fileName;

        // 将图片保存到指定路径
        Files.write(Paths.get(filePath), imageBytes);

        // 打印保存路径
        System.out.println("图片已保存到：" + filePath);

        // 如果需要，可以使用 Desktop 类打开图片
        // Desktop.getDesktop().open(new File(filePath));
    }

    @Test
    void test2(){
        // Markush list
        List<String> markushList = new ArrayList<>();
        markushList.add("[R7]C1C2C3(C)CCCC(C)(C)C3CCC2(C)c2c([R4])c([R3])c([R2])c([R1])c2S1(=O)=O"); // 5
        markushList.add("C1SCCCC1"); // 1
        markushList.add("C1[*]CCCC1"); // 2
        markushList.add("[R7]C1SC2=C(C3(CCC4C(C)(CCCC4(C13)C)C)C)C([R4])=C(C([R2])=C2[R1])[R3]"); // 6
        markushList.add("[R7]C1C2C3(CCCC(C)(C3CCC2(C4=C(S1=O)C([R1])=C(C([R3])=C4[R4])[R2])C)C)C"); // 7
        markushList.add("[R7]C1[X]c2c([R1])c([R2])c([R3])c([R4])c2C3(C)CCC4C(CCCC4(C)C13)(C)C"); // 8
        markushList.add("[R1]C1=CC=CC=C1[R2]"); // 3

        // Instance list
        List<String> instanceList = new ArrayList<>();
        instanceList.add("CC1=CC2=C(C(=C1)O)[C@@]3(CC[C@@H]4[C@@]([C@H]3CS2(=O)=O)(CCCC4(C)C)C)C"); // 匹配 5
        instanceList.add("C[C@]12CCCC([C@@H]1CC[C@@]3([C@@H]2CS(=O)(=O)C4=C3C(=CC(=C4)OC)OC)C)(C)C"); // 6
        instanceList.add("C[C@]12CCCC([C@@H]1CC[C@@]3([C@@H]2CS(=O)C4=C3C(=CC(=C4)OC)OC)C)(C)C"); // 7
        instanceList.add("C[C@]12CCCC([C@@H]1CC[C@@]3([C@@H]2CS(=O)C4=C3C(=CC(=C4)OC)OC)C)(C)C"); // 7
        instanceList.add("CC1=CC(=CC2=C1[C@@]3(CC[C@@H]4[C@@]([C@H]3CS2)(CCCC4(C)C)C)C)O"); // 6
        instanceList.add("CC(C)OC1=CC2=C(C(=C1)OC(C)C)[C@@]3(CC[C@@H]4[C@@]([C@H]3CO2)(CCCC4(C)C)C)C"); // 8
        instanceList.add("CC(OC1=C(C)C2=C([C@]3(C)CC[C@@H]4[C@@](CC(C)CC4(C)C)(C)[C@H]3CO2)C(OC(C)C)=C1)C");
        instanceList.add("C1OCCCC1");
        instanceList.add("C1SCCCC1");
        instanceList.add("CC1=CC=CC=C1C");
        instanceList.add("CC1=CC=CC(C)=C1");
        instanceList.add("CC1=CC=C(C)C=C1");
        String marAndNum = ApiUtil.getMarAndNum(markushList, instanceList);
        System.out.println(marAndNum);
    }

    @Test
    void test3() {
        List<NumeratorPo> numeratorPoList = new ArrayList<>();
        NumeratorPo numeratorPo = new NumeratorPo();
        numeratorPoList.add(numeratorPo);
        numeratorPo.setId(100);
        for (NumeratorPo po : numeratorPoList) {
            System.out.println(po);
        }
    }

    @Test
    void test4() {
        JSONObject jsonObject=new JSONObject("{\n" +
                "        \"SMILES\": [\n" +
                "            \"[R]O/N=[N+](/[O-])N1CCC[C@H]1C(=O)Oc1c(C)ncc2c1CO[C@H]2c1ccc(Cl)cc1\",\n" +
                "            \"CCCC(C)C(C)C\",\n" +
                "            \"CCCCCC(C)C\",\n" +
                "            \"CC[C@H](C)CC(C)C\",\n" +
                "            \"CC(C)CCC(C)C\",\n" +
                "            \"CC(C)C1CCCCC1\"\n" +
                "        ],\n" +
                "        \"img_path\": \"images/Tables/6_74_94_293_469_table.jpg\",\n" +
                "        \"page_idx\": 6,\n" +
                "        \"table_SMILES\": [],\n" +
                "        \"table_body\": \"\\n\\n<html><body><table><tbody><tr><td colspan=\\\"4\\\">$SMILES$[R]O/N=[N+](/[O-])N1CCC[C@H]1C(=O)Oc1c(C)ncc2c1CO[C@H]2c1ccc(Cl)cc1$/SMILES$</td><td><b>example_no</b></td><td>R</td><td>HPLC R</td><td>MS (M + H)</td></tr><tr><td>7</td><td>$SMILES$CCCCCC(C)C$/SMILES$</td><td>2.21</td><td>489.4</td></tr><tr><td>8</td><td>$SMILES$CC(C)CCC(C)C$/SMILES$</td><td>2.19</td><td>489.4</td></tr><tr><td>9</td><td>$SMILES$CCCC(C)C(C)C$/SMILES$</td><td>2.32</td><td>489.4</td></tr><tr><td>10</td><td>$SMILES$CC[C@H](C)CC(C)C$/SMILES$</td><td>2.19</td><td>489.38</td></tr><tr><td>11</td><td>$SMILES$CC(C)C1CCCCC1$/SMILES$</td><td>3.51</td><td>501.08</td></tr></tbody></table></body></html>\\n\\n\",\n" +
                "        \"table_caption\": [\n" +
                "            \"-continued \"\n" +
                "        ],\n" +
                "        \"table_footnote\": [],\n" +
                "        \"type\": \"table\"\n" +
                "    }");
        Object smiles = jsonObject.opt("SMILES");
        if (smiles instanceof String) {
            String smilesStructure = jsonObject.optString("SMILES");
            System.out.println(smilesStructure);
        } else {
            JSONArray smilesJsonArray = jsonObject.optJSONArray("SMILES");
            for (int i = 0; i < smilesJsonArray.length(); i++) {
                String string = smilesJsonArray.getString(i);
                System.out.println(string);
            }
        }

    }
}
